Lua Plugin Snippets

Besides these examples, you'll want to read http://flyingmeat.com/fs/contrib/voodoopad/VPPlugin.h for information on what is available. It's just an Objective-C header file, but it's what we've got to go on for now :)

List all the page names in the current document to the current page.

textView = windowController:textView()
document = windowController:document()
enum = document:keys():objectEnumerator()
pageKey = enum:nextObject()
while pageKey do
    pageKey = nsToLuaString(enum:nextObject())
    textView:insertText(pageKey .. "\n")
end

Creating a new page named "Lua Rocks"

document = windowController:document()
textView = windowController:textView()
document:createNewVPDataWithKey("Lua Rocks")
textView:insertText("Lua Rocks")

Printing out the contents of the index page (to Console.app)

document = windowController:document()
vpdata   = document:vpDataForKey("index")
pageText = nsToLuaString(vpdata:dataAsAttributedString():string())
print(pageText)

Lookup the page data for the current page, and change it's contents.

(Note: this is not undoable)

document = windowController:document()
currentPageKey = windowController:key()
pageData = document:vpDataForKey(currentPageKey)
attributedString = pageData:dataAsAttributedString()
attributedString:mutableString():setString("It's all gone!")
pageData:setDataAsAttributedString(attributedString)

Tell the text view to select all, and then insert the text "Hi".

(This is undoable)

windowController:textView():selectAll()
windowController:textView():insertText("Hi")

Display a dialog box.

This example displays a dialog box with a default button that says "Go Away"

nsAlert("Hello", "This is a message", "Go Away", nil, nil)

Highlight the selected text yellow.

(this will work best as a plugin, since the Run as Lua Script option deselects the currently selected text before running the script)

textView = windowController:textView()
color = objc.class("NSColor"):yellowColor()
textView:textStorage():addAttribute_value_range_("NSBackgroundColor", color, textView:selectedRange())

Create a page index.

document        = windowController:document()
textView        = windowController:textView()
pageKey         = nil
enum            = document:keys():objectEnumerator()
pageKey         = enum:nextObject()
while pageKey do
    -- the display name isn't currently public in 2.5.1, but I'll try and fix that for 2.5.2
    displayName = document:vpDataForKey(pageKey):displayName()
    textView:insertText(displayName)
    textView:insertText("\n")
    pageKey        = enum:nextObject()
end

Change the font for the current page.

textView = windowController:textView()
f = objc.class("NSFont"):fontWithName_size("Monaco", 10)
attrs = objc.class("NSMutableDictionary"):alloc():init():autorelease()
attrs:setObject_forKey(f, "NSFont")
range = luaToNSRange({location=0, length=textView:string():length()})
textView:textStorage():setAttributes_range(attrs, range)

Change the font for the current selection.

textView = windowController:textView()
font = objc.class("NSFont"):fontWithName_size("Helvetica", 14)
textView:textStorage():addAttribute_value_range_("NSFont", font, textView:selectedRange())